package com.pktechan.api.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pktechan.api.entity.MsgCode;
import com.pktechan.api.exception.CustomException;
import com.pktechan.api.service.MsgCodeService;
import com.pktechan.api.service.RedisService;
import com.pktechan.api.utils.HtmlUtil;
import com.pktechan.api.utils.ImageCode;
import com.pktechan.api.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: cloudfloating
 * @description: 验证码
 * @author: qx.zhang
 * @create: 2021-03-05 11:27
 **/
@RestController
@RequestMapping("msgCodeController")
public class MsgCodeController {

    private static final Logger logger = LoggerFactory.getLogger(MsgCodeController.class);

    @Autowired
    private MsgCodeService msgCodeService;
    @Autowired
    private RedisService redisService;

    @RequestMapping("getMsgCodeById")
    public void getMsgCodeById(Integer id, HttpServletResponse response) {
        try {
            MsgCode msgCode = msgCodeService.getById(id);
            HtmlUtil.writerSuccessJson(response, msgCode);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }


    @RequestMapping("saveCustBase")
    public void saveCustBase(MsgCode msgCode, HttpServletResponse response) {
        try {
            logger.info("添加消息");
            msgCodeService.save(msgCode);
            HtmlUtil.writerSuccessJson(response);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    @RequestMapping("updateCustBase")
    public void updateCustBase(MsgCode msgCode, HttpServletResponse response) {
        try {
            logger.info("修改到消息 id= " + msgCode.getId());
            msgCodeService.update(msgCode);
            HtmlUtil.writerSuccessJson(response);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 分页查询List
     *
     * @param msgCode
     * @return
     */
    @RequestMapping("getMsgCodeList")
    public void getMsgCodeList(MsgCode msgCode, HttpServletResponse response) {
        try {
            logger.info("消息列表");
            PageHelper.startPage(msgCode.getPageNo(), msgCode.getPageSize());
            List<MsgCode> list = msgCodeService.getList(msgCode);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response, pageInfo);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    @RequestMapping("sendMessageCode")
    public void sendMessageCode(@RequestParam String mobile, HttpServletResponse response) {
        try {
            MsgCode msgCode = msgCodeService.sendMessageCode(mobile);
            HtmlUtil.writerSuccessJson(response, msgCode);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 获取图片验证码
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/getImageAuthCode")
    public void getImageAuthCode(@RequestParam String userLoginState, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ImageCode imageCode = new ImageCode();
        StringBuffer sb = new StringBuffer();
        imageCode.doGet(sb, request, response);
        redisService.setImageAuthCode(userLoginState, sb.toString());
    }

    /**
     * 图片验证码验证
     *
     * @param imgCode
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/imageCodeVerify")
    public void imgVerification(@RequestParam String userLoginState, @RequestParam String imgCode, HttpServletRequest request, HttpServletResponse response) {
        String imageAuthCode = redisService.getImageAuthCode(userLoginState);
        if (StringUtil.equals(imgCode, imageAuthCode)) {
            HtmlUtil.writerSuccessJson(response);
        } else {
            HtmlUtil.writerFailJson(response);
        }
    }


    /**
     * 获取实名认证图片验证码
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/getRealNameImageAuthCode")
    public void getRealNameImageAuthCode(@RequestParam String userLoginState, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ImageCode imageCode = new ImageCode();
        StringBuffer sb = new StringBuffer();
        imageCode.doGet(sb, request, response);
        redisService.setRealNameImageAuthCode(userLoginState, sb.toString());
    }
}
